package com.dpj.book.pojo;

import java.util.Map;
import java.util.Set;

public class Cart {
    private Map<Integer,CartItem> cartItemMap; //购物车中 购物车项的合集，这个map集合中的kep是book的id

    private Double totalMoney; //购物车的总金额

    private Integer totalCount; //购物车中购物项的数量

    public Cart() {
    }

    public Cart(Map<Integer, CartItem> cartItemMap, Double totalMoney, Integer totalCount) {
        this.cartItemMap = cartItemMap;
        this.totalMoney = totalMoney;
        this.totalCount = totalCount;
    }

    public Map<Integer, CartItem> getCartItemMap() {
        return cartItemMap;
    }

    public void setCartItemMap(Map<Integer, CartItem> cartItemMap) {
        this.cartItemMap = cartItemMap;
    }

    public Double getTotalMoney() {
        totalMoney=0.0;
        if(cartItemMap!=null&&cartItemMap.size()>0){
            Set<Map.Entry<Integer, CartItem>> entries = getCartItemMap().entrySet();
            for (Map.Entry<Integer, CartItem> entry : entries) {
                CartItem carItem = entry.getValue();
                totalMoney=totalMoney+carItem.getBook().getPrice()*carItem.getBuyCount();
            }
        }
        return totalMoney;
    }
    public void setTotalMoney(Double totalMoney) {

        this.totalMoney = totalMoney;
    }

    public Integer getTotalCount() {
        totalCount=0;
        if(cartItemMap!=null&&cartItemMap.size()>0){
            totalCount=cartItemMap.size();
        }
        return totalCount;
    }

    public void setTotalCount(Integer totalCount) {

        this.totalCount = totalCount;
    }

    @Override
    public String toString() {
        return "Cart{" +
                "cartItemMap=" + cartItemMap +
                ", totalMoney=" + totalMoney +
                ", totalCount=" + totalCount +
                '}';
    }
}
